<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class Dbgeneric_Model extends CI_Model {
	
	private $table= '';
	function __construct() {
		parent::__construct();
		$this->clientModel = $this->load->model('Client_Model'); 
	}
	
	function saveOrUpdate($table,$data,$key){
		if(isset($data["$key"]) && $data["$key"] != ''){
			$exist = $this->find($table, array("$key"=> $data["$key"]));
			if(!empty($exist)){
				$value = $data["$key"];
				unset($data["$key"]);
				$this->db->where("$key", $value);
				$this->db->update($table, $data);
				return true;
			} else {
				$this->db->insert($table, $data);
				return $this->db->insert_id();
			}

		}else{
			$this->db->insert($table, $data);
			return $this->db->insert_id();
		}
		
	}
	
	function find($table, $array){
		$query = $this->db->get_where($table, $array);
    	$result = $query->result();
    	return $result;
	}
	
	function saveOrUpdateArray($table,$data,$key){
		$params = array();
		foreach($key as $k){
			$params["$k"] = $data["$k"];
		}
		
		if(!empty($params)){
		
			$exist = $this->find($table, $params);
			if(!empty($exist)){
				foreach($params as $k=>$val){
					$this->db->where("$k", $val);
					unset($data["$k"]);
				}
				$this->db->update($table, $data);
				return true;
			} else {
				$this->db->insert($table, $data);
				return $this->db->insert_id();
			}
			
		}else{
			$this->db->insert($table, $data);
			return $this->db->insert_id();
		}
		
		
	}
	
	
}